package com.sgb.member.ui.fragment

import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.annotation.NonNull
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.sgb.member.R
import com.sgb.member.databinding.DialogMemberChangeLevelBinding

/**
 * 会员调级dialog
 */
class MemberChangeLevelDialog(val company:String,var level:String ="",val callBack: (String) -> Unit) : DialogFragment() {
    lateinit var mBinding: DialogMemberChangeLevelBinding
    val data_list = listOf("一星会员", "二星会员", "三星会员", "四星会员", "五星会员", "普通会员")
    lateinit var arr_adapter: ArrayAdapter<String>
    var isFirst = true
    override fun onCreateView(
        inflater: LayoutInflater, container:
        ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(
            inflater, R.layout.dialog_member_change_level,
            container, false
        )
        mBinding.tvQueDing.setOnClickListener {
            if(mBinding.tvLeve.text ==level){
                mBinding.tvTip.visibility = View.VISIBLE
             return@setOnClickListener
            }
            callBack.invoke(level)
            dismiss()
        }
        mBinding.tvCancel.setOnClickListener {
            dismiss()
        }
        mBinding.tvCompanyName.text =company
        mBinding.v.visibility=if(company.length>10)View.GONE else View.VISIBLE
        mBinding.tvLeve.text = level
        if(level=="一星会员"){
            mBinding.ivStar1.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
        }else if(level=="二星会员"){
            mBinding.ivStar1.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar2.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
        }else if(level=="三星会员"){
            mBinding.ivStar1.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar2.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar3.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
        }else if(level=="四星会员"){
            mBinding.ivStar1.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar2.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar3.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar4.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
        }else if(level=="五星会员"){
            mBinding.ivStar1.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar2.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar3.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar4.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
            mBinding.ivStar5.setImageDrawable(context?.resources?.getDrawable(R.drawable.member_start))
        }
        arr_adapter = ArrayAdapter(activity, R.layout.my_simple_spinner_item, data_list)
        arr_adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item)
        mBinding.spinner.adapter = arr_adapter
        mBinding.spinner.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {

                mBinding.tvTip.visibility = View.INVISIBLE
                level = mBinding.spinner.selectedItem as String
                Log.i("xxx","level"+ level)
            }
            override fun onNothingSelected(parent: AdapterView<*>?) {}

        }
        return mBinding.root
    }

    @NonNull
    @Override
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return Dialog(requireActivity(), R.style.iosDialog).apply { isCancelable = false }
    }

}